package com.data.codegenerate.generate.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.data.codegenerate.config.CodeConfigProperties;
import com.data.codegenerate.database.DbReadTableUtil;
import com.data.codegenerate.generate.IGenerate;
import com.data.codegenerate.generate.config.CreateFileConfig;
import com.data.codegenerate.generate.impl.base.BaseCodeGenerate;
import com.data.codegenerate.generate.pojo.ColumnVo;
import com.data.codegenerate.generate.pojo.onetomany.MainTableVo;
import com.data.codegenerate.generate.pojo.onetomany.SubTableVo;
import com.data.codegenerate.generate.util.NonceUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CodeGenerateOneToMany extends BaseCodeGenerate implements IGenerate {
    public static String a = "A";
    public static String b = "B";
    private static final Logger e = LoggerFactory.getLogger(CodeGenerateOneToMany.class);
    private static String f;
    private static DbReadTableUtil k = new DbReadTableUtil();
    private MainTableVo g;
    private List<ColumnVo> h;
    private List<ColumnVo> i;
    private List<SubTableVo> j;

    public CodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
        this.j = subTables;
        this.g = mainTableVo;
    }

    public CodeGenerateOneToMany(MainTableVo mainTableVo, List<ColumnVo> mainColums, List<ColumnVo> originalMainColumns, List<SubTableVo> subTables) {
        this.g = mainTableVo;
        this.h = mainColums;
        this.i = originalMainColumns;
        this.j = subTables;
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public Map<String, Object> a() throws Exception {
        int i2 = -1;
        HashMap hashMap = new HashMap();
        hashMap.put("bussiPackage", CodeConfigProperties.bussiPackage);
        hashMap.put("entityPackage", this.g.getEntityPackage());
        hashMap.put("entityName", this.g.getEntityName());
        hashMap.put("tableName", this.g.getTableName());
        hashMap.put("ftl_description", this.g.getFtlDescription());
        hashMap.put("primaryKeyField", CodeConfigProperties.dbTableId);
        if (this.g.getFieldRequiredNum() == null) {
            this.g.setFieldRequiredNum(Integer.valueOf(StringUtils.isNotEmpty(CodeConfigProperties.fieldRequiredNum) ? Integer.parseInt(CodeConfigProperties.fieldRequiredNum) : -1));
        }
        if (this.g.getSearchFieldNum() == null) {
            MainTableVo mainTableVo = this.g;
            if (StringUtils.isNotEmpty(CodeConfigProperties.pageSearchFiledNum)) {
                i2 = Integer.parseInt(CodeConfigProperties.pageSearchFiledNum);
            }
            mainTableVo.setSearchFieldNum(Integer.valueOf(i2));
        }
        if (this.g.getFieldRowNum() == null) {
            this.g.setFieldRowNum(Integer.valueOf(Integer.parseInt(CodeConfigProperties.fieldRowNum)));
        }
        hashMap.put("tableVo", this.g);
        try {
            if (this.h == null || this.h.size() == 0) {
                this.h = DbReadTableUtil.a(this.g.getTableName());
            }
            if (this.i == null || this.i.size() == 0) {
                this.i = DbReadTableUtil.readOriginalTableColumn(this.g.getTableName());
            }
            hashMap.put("columns", this.h);
            hashMap.put("originalColumns", this.i);
            for (ColumnVo columnVo : this.i) {
                if (columnVo.getFieldName().toLowerCase().equals(CodeConfigProperties.dbTableId.toLowerCase())) {
                    hashMap.put("primaryKeyPolicy", columnVo.getFieldType());
                }
            }
            for (SubTableVo subTableVo : this.j) {
                if (subTableVo.getColums() == null || subTableVo.getColums().size() == 0) {
                    subTableVo.setColums(DbReadTableUtil.a(subTableVo.getTableName()));
                }
                if (subTableVo.getOriginalColumns() == null || subTableVo.getOriginalColumns().size() == 0) {
                    subTableVo.setOriginalColumns(DbReadTableUtil.readOriginalTableColumn(subTableVo.getTableName()));
                }
                String[] foreignKeys = subTableVo.getForeignKeys();
                ArrayList arrayList = new ArrayList();
                for (String str : foreignKeys) {
                    arrayList.add(DbReadTableUtil.d(str));
                }
                subTableVo.setForeignKeys((String[]) arrayList.toArray(new String[0]));
                subTableVo.setOriginalForeignKeys(foreignKeys);
            }
            hashMap.put("subTables", this.j);
            hashMap.put("serialVersionUID", String.valueOf(NonceUtils.c() + NonceUtils.g()));
            e.info("code template data: " + hashMap.toString());
            return hashMap;
        } catch (Exception e2) {
            throw e2;
        }
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public List<String> generateCodeFile(String stylePath) throws Exception {
        String str = CodeConfigProperties.projectPath;
        Map<String, Object> a2 = a();
        String str2 = CodeConfigProperties.templatepath;
        if (a(str2, "/").equals("conf/code-template")) {
            str2 = "/" + a(str2, "/") + "/onetomany";
            CodeConfigProperties.getUrl(str2);
        }
        CreateFileConfig aVar = new CreateFileConfig(str2);
        aVar.a(stylePath);
        a(aVar, str, a2);
        e.info("----- jeecg-boot ---- generate  code  success =======> 主表名：" + this.g.getTableName());
        return this.d;
    }

    @Override // org.jeecgframework.codegenerate.generate.IGenerate
    public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
        if (projectPath != null && !"".equals(projectPath)) {
            CodeConfigProperties.getDriverName(projectPath);
        }
        if (templatePath != null && !"".equals(templatePath)) {
            CodeConfigProperties.getUrl(templatePath);
        }
        generateCodeFile(stylePath);
        return this.d;
    }
}
